package com.qc.common;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;


public class FormatObjToBean {
	private static List<String> ObjectArr = new ArrayList<String>();
	private static Logger logger = Logger.getLogger(FormatObjToBean.class);
	
	static {
		ObjectArr.add("UserEntity");
	}

	private final static String PACKAGE_NAME = "com.qc.entity.";

	/**
	 * parseDataSourceForList:将结果集转换成list
	 *
	 * @param  @param className
	 * @param  @param rs
	 * @param  @return
	 * @return ArrayList<?>
	 * @throws 
	*/
	public static ArrayList<?> parseDataSourceForList(String className, ResultSet rs) {
		try {
			ArrayList<Object> objList = new ArrayList<Object>();

			while (rs.next()) {
				Class<?> clazz = Class.forName(PACKAGE_NAME + className);
				Object o = clazz.newInstance();
				Field[] fields = clazz.getDeclaredFields();
				for (Field field : fields) {
					String fieldName = field.getName();
					String fieldObjectName = field.getType().getSimpleName();
					Method method = clazz.getMethod("set"+fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), field.getType());
					if (ObjectArr.contains(fieldObjectName)) {
						method.invoke(o,
								parseDataSourceForList(fieldObjectName, rs));
					} else {
						try {
							method.setAccessible(true);
							method.invoke(
									o, fieldObjectName.equals("int") ? rs.getInt(fieldName) : fieldObjectName.equals("float") ? rs.getFloat(fieldName) : rs.getObject(fieldName));
						} catch (Exception e) {
							e.printStackTrace();
							logger.info(fieldName + "　can't find");
						}
					}
				}
				objList.add(o);
			}
			return objList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * parseDataSourceForObj:将结果集转换成object
	 *
	 * @param  @param className
	 * @param  @param rs
	 * @param  @return
	 * @return Object
	 * @throws 
	*/
	public static Object parseDataSourceForObj(String className, ResultSet rs) {
		try {
			Class<?> clazz = Class.forName(PACKAGE_NAME + className);
			Object o = clazz.newInstance();
			Field[] fields = clazz.getDeclaredFields();
			for (Field field : fields) {
				String fieldName = field.getName();
				String fieldObjectName = field.getType().getSimpleName();
				Method method = clazz.getMethod("set"+fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), field.getType());
				if (ObjectArr.contains(fieldObjectName)) {
					method.invoke(o,
							parseDataSourceForObj(fieldObjectName, rs));
				} else {
					try {
						method.setAccessible(true);
						method.invoke(
								o, rs.getObject(fieldName));
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println(fieldName + "　can't find");
					}
				}
			}
			return o;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}

